package com.tool.btn;

import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import org.apache.commons.lang.StringUtils;
import org.come.Jpanel.LingHelpListJpanel;
import org.come.Jpanel.SupportListJpanel;
import org.come.model.Lingbao;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class LingHelpBtn extends MoBanBtn {
    //0上 1下 2顶 3底
    private LingHelpListJpanel jpanel;
    private int p;
    public LingHelpBtn(String iconpath, int type, int p, LingHelpListJpanel jpanel) {
        super(iconpath, type);
        // TODO Auto-generated constructor stub
        this.p=p;
        this.jpanel=jpanel;


    }

    public LingHelpBtn(String iconpath, int type, int p, String text, LingHelpListJpanel jpanel) {
        // TODO Auto-generated constructor stub
        super(iconpath, type, UIUtils.COLOR_BTNTEXT);
        this.setText(text);
        this.setFont(UIUtils.TEXT_FONT);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.p=p;
        this.jpanel = jpanel;
    }
    @Override
    public void chooseyes() {
        // TODO Auto-generated method stub

    }

    @Override
    public void chooseno() {
        // TODO Auto-generated method stub

    }

    @Override
    public void nochoose(MouseEvent e) {
        // TODO Auto-generated method stub
        //获取当前选中的位置
        int w=jpanel.getListpet().getSelectedIndex();
        if (w==-1) {
            return;
        }
        RoleData data=RoleData.getRoleData();
        List<String> list=null;
        //0上 1下 2顶 3底
        if (p==0) {
            int i = w - 1;
            Lingbao lingbao = jpanel.getListModel().get(i);
            Lingbao lingbao1 = jpanel.getListModel().get(w);
            jpanel.getListModel().set(i, lingbao1);
            jpanel.getListModel().set(w, lingbao);
            jpanel.getListpet().setSelectedIndex(i);
//            list=data.CHelpBb(w, w-1);
        }else if (p==1) {
            int i = w + 1;
            Lingbao lingbao = jpanel.getListModel().get(i);
            Lingbao lingbao1 = jpanel.getListModel().get(w);
            jpanel.getListModel().set(i, lingbao1);
            jpanel.getListModel().set(w, lingbao);
            jpanel.getListpet().setSelectedIndex(i);
//            list=data.CHelpBb(w, w+1);
        }else if (p==2) {
            int i = 0;
            Lingbao lingbao = jpanel.getListModel().get(i);
            Lingbao lingbao1 = jpanel.getListModel().get(w);
            jpanel.getListModel().set(i, lingbao1);
            jpanel.getListModel().set(w, lingbao);
            jpanel.getListpet().setSelectedIndex(i);
//            list=data.CHelpBb(w, w+1);
        }else {

            int i = jpanel.getListModel().getSize()-1;
            Lingbao lingbao = jpanel.getListModel().get(i);
            Lingbao lingbao1 = jpanel.getListModel().get(w);
            jpanel.getListModel().set(i, lingbao1);
            jpanel.getListModel().set(w, lingbao);
            jpanel.getListpet().setSelectedIndex(i);
//            list=data.CHelpBb(w, w+1);
        }
//        if (list!=null) {
////            jpanel.init(list);
//        }
        initLings();

    }
    private  void  initLings(){
        List<Lingbao> lings = new ArrayList<>();
        for (int j = 0; j < jpanel.getListModel().getSize(); j++) {
            lings.add(jpanel.getListModel().get(j));
        }
        if(lings.size()>0){
            BigDecimal[] array = lings.stream().map(Lingbao::getBaoid).toArray(BigDecimal[]::new);
            String join = StringUtils.join(array, "|");
            RoleData.getRoleData().sendHelpLingbao(join);
        }
//        jpanel.init(lings);
    }

}
